<?php
namespace Home\Controller;
use \Think\Controller;
date_default_timezone_set('PRC');


/**
* 发布经验
*/
class ExperienceController extends SimpleController
{
    private $path;
    function index()
    {
        if (empty($_SESSION['user'])) {
            $this->error('发布经验请先登录哦',U('Home/Login/index'),1);
            exit;
        }
        $this->display();
    }

    function category()
    {
        $model = M('category');
        //接收查询条件
        $pid = empty($_GET['pid'])?0:$_GET['pid'];
        //查询
        $data = $model->where('pid='.$pid)->select();
        echo json_encode($data);
    }


    function experience()
    {
        // $config = array(
        // 'maxSize' => 3145728,
        // 'rootPath' => '/public/Uploads/experience',
        // 'savePath' => '',
        // 'saveName' => array('uniqid',''),
        // 'exts' => array('jpg', 'gif', 'png', 'jpeg'),
        // 'autoSub' => true,
        // 'subName' => array('date','Ymd'),
        // );
        if (empty($_POST['content'])){
            $this->error('您忘记写内容啦！！！');
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/experience/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
        }else{// 上传成功
            if ($_POST['category1'] == -1) {
                $this->error('请选择分类');
            }

            if (!empty($_POST['category4'])){
                $_POST['category_id'] = $_POST['category4'];
            }elseif(!empty($_POST['category3'])){
                $_POST['category_id'] = $_POST['category3'];
            }elseif(!empty($_POST['category2'])){
                $_POST['category_id'] = $_POST['category2'];
            }else{
                $_POST['category_id'] = $_POST['category1'];
            }
        $path = "/Public/Uploads/experience/".$info['photo']['savename'];
        $image = new \Think\Image();
        $image->open('.'.$path);
        // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg

        $image->thumb(352, 220,\Think\Image::IMAGE_THUMB_CENTER)->save("./Public/Uploads/experience/_s_".$info['photo']['savename']);
        $_POST['_s_picture'] = "/Public/Uploads/experience/_s_".$info['photo']['savename'];

        $_POST['picture'] = $path;
        $_POST['uid'] = $_SESSION['user']['uid'];
        $_POST['uname'] = $_SESSION['user']['name'];
        $_POST['create_time'] = NOW_TIME;
        $_POST['ctime'] = date('Y年m月d日 H:i:s');
        $model = M('experience');
        $model->create();
        if (!$model->add()) {
            $this->error('抱歉，发布失败');
        }else{
            M('user_info')->where('uid = '.$_SESSION['user']['uid'])->setInc('exp','20');
            $this->success('恭喜，发布成功',U('Index/index'));
        }

        }
    }

}